var c = sitedata.getComponent(url.templateArgs.componentId);

var title = String(json.get("title"));
var ytUrl = String(json.get("ytUrl"));
var start = String(json.get("start"));

function getQueryStringParameter(paramName, url) {

    var i, len, idx, queryString, params, tokens;

    url = url || top.location.href;

    idx = url.indexOf("?");
    queryString = idx >= 0 ? url.substr(idx + 1) : url;

    // Remove the hash if any
    idx = queryString.lastIndexOf("#");
    queryString = idx >= 0 ? queryString.substr(0, idx) : queryString;

    params = queryString.split("&");

    for (i = 0, len = params.length; i < len; i++) {
        tokens = params[i].split("=");
        if (tokens.length >= 2) {
            if (tokens[0] === paramName) {
                return unescape(tokens[1]);
            }
        }
    }

    return null;
}

//check if the URL is modified
if (ytUrl.indexOf("http://www.youtube.com/embed/") == -1){
	var youtubeId = getQueryStringParameter("v", ytUrl);
	if (youtubeId){
		ytUrl = "http://www.youtube.com/embed/" + youtubeId + "?wmode=opaque";
	}
}

var startTime = getQueryStringParameter("start", ytUrl);
if (start) {
	if (startTime)
		ytUrl = ytUrl.replace("start=" + startTime, "start=" + start);
	else
		ytUrl += "&start=" + start;
} else if (startTime)
	ytUrl = ytUrl.replace("&start=" + startTime, "");
else
	ytUrl = ytUrl.replace("&start=" + startTime, "");

c.properties["ytUrl"] = ytUrl;
c.properties["title"] = title;
c.properties["start"] = start;

model.title = title;
model.start = start;
model.ytUrl = (ytUrl == "") ? null : ytUrl;

c.save();
